package com.example.esp8266.activity.utile;

public class TranscodingUtils {

    //十六进制转字符串
    public static String stringToGbk(String string) throws Exception{
        byte[] bytes = new byte[string.length() / 2];
        for(int i = 0; i < bytes.length; i ++){
            byte high = Byte.parseByte(string.substring(i * 2, i * 2 + 1), 16);
            byte low = Byte.parseByte(string.substring(i * 2 + 1, i * 2 + 2), 16);
            bytes[i] = (byte) (high << 4 | low);
        }
        String result = new String(bytes, "gbk");
        return result;
    }

    //十六进制转十进制
    public static String bytes2hex03(byte[] bytes) {

        final String HEX = "0123456789abcdef";

        StringBuilder sb = new StringBuilder(bytes.length * 2);

        for (byte b : bytes) {

            sb.append(HEX.charAt((b >> 4) & 0x0f));

            sb.append(HEX.charAt(b & 0x0f));

        }
        return sb.toString();
    }


    //十进制转十六进制
    public static String toHex(long num, int totalLenght) {
        String str = Long.toHexString(num);
        str = splicingZero(str, totalLenght);
        return str;
    }

    public static String splicingZero(String str, int totalLenght) {
        int strLenght = str.length();
        String strReturn = str;
        for (int i = 0; i < totalLenght - strLenght; i++) {

            strReturn = "0" + strReturn;
        }
        return strReturn;
    }





}
